<template>
  <PageWrapper>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="customHeader"> 导出表格 </a-button>
      </template>
    </BasicTable>
  </PageWrapper>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';
  import { BasicTable, useTable } from '/@/components/Table';
  import { PageWrapper } from '/@/components/Page';
  import { getBasicColumns, searchFormSchema } from './orderStatisticsHotelData_business';
  import { getHotelOrderStatisticsBussess } from '/@/api/orderStaiistics/hotel/hotelOrderStaiisticsAPI';
  import { jsonToSheetXlsx } from '/@/components/Excel';
  import { toRaw, ref } from 'vue';
  import dayjs from 'dayjs';
  export default defineComponent({
    components: { BasicTable, PageWrapper },
    setup() {
      const [registerTable, { getDataSource }] = useTable({
        api: getHotelOrderStatisticsBussess,
        title: '酒店订单（有审批单）',
        columns: getBasicColumns(),
        formConfig: {
          labelWidth: 100,
          actionColOptions: { span: 24 },
          schemas: searchFormSchema,
          fieldMapToTime: [
            ['arrivalDate', ['arrivalDateStart', 'arrivalDateEnd'], 'YYYY-MM-DD'],
            ['departureDate', ['departureDateStart', 'departureDateEnd'], 'YYYY-MM-DD'],
            ['createTime', ['createTimeStart', 'createTimeEnd'], 'YYYY-MM-DD'],
          ],
        },
        pagination: { pageSize: 10 },
        expandRowByClick: true,
        striped: true,
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: true,
        tableSetting: { fullScreen: true },
      });
      function getDays(startDateString, endDateString) {
        const start = new Date(startDateString);
        const end = new Date(endDateString);
        const millisecondsPerDay = 1000 * 60 * 60 * 24;
        // 一天的毫秒数
        const millisecondsDiff = end.getTime() - start.getTime();
        // 获取毫秒差值
        const daysDiff = Math.ceil(millisecondsDiff / millisecondsPerDay);
        // 计算相差的天数
        return daysDiff;
      }
      function customHeader() {
        let header = {};
        let orderArr: object[] = [];

        for (let i = 0; i < getBasicColumns().length; i++) {
          // console.log(getBasicColumns()[i].title);
          let key: string = (getBasicColumns()[i].dataIndex as string) ?? '';
          header[key] = getBasicColumns()[i].title;
        }
        header['id'] = '序号';
        console.log(header);
        for (let k = 0; k < toRaw(getDataSource()).length; k++) {
          let data = ref({
            id: 1,
            CCSQDH: '出差申请单号',
            DDH: '订单号',
            DDLX: '订单类型',
            DDQK: '订单情况',
            GYSDH: '供应商单号',
            RZRBM: '入住人部门',
            SBYKK: 0, // 深白云扣款
            TKJE: '退款金额',
            YDR: '预订人',
            YDRBM: '预订人部门',
            YSZJ: '应收总计',
            ZDLX: '终端类型',
            ZJEBHFWD: '总金额包含服务费',
            accountNo: '预订人编号',
            arrivalDate: '入住日期',
            busiManage: '业务经理',
            certNo: '入住人卡号',
            channelName: '渠道',
            chucairenshu: '出差人数',
            costDeptName: '成本中心',
            createTime: '预订时间',
            departureDate: '离店日期',
            geoName: '城市',
            hotelName: '酒店名称',
            jianyeshu: '间夜数',
            mobile: '联系人电话',
            money: '订单金额',
            names: '入住人',
            // orderId: '订单号',
            payType: '支付方式',
            amount: '个人支付',
            refundAmount: '个人退款',
            companyAmount: '公司支付',
            refundCompanyAmount: '公司退款',
            serialNum: '支付流水号',
            priceAvg: '日均价',
            roomCount: '房间数',
            roomName: '房型',
            status: '订单状态',
            travelType: '预订类型',
            voucherId: 'OA申请单号',
            recordId: '消费记录id',
            hotelLeave: '舒适型', // 酒店级别
            1: '0',
            2: '0',
            3: '0',
            4: '0',
            5: '0',
            6: '0',
            7: '0',
            8: '0',
            9: '-',
            10: '-',
            11: '-',
            17: '-',
            18: '-',
            19: '-',
            20: '-',
            21: '-',
            22: 'cbje',
            23: '-',
            24: '-',
            25: '-',
            26: '-',
            27: '-',
            28: '-',
            29: '-',
            30: '-',
            31: '-',
            32: '-',
            33: '-',
            34: '-',
            35: '-',
            36: '-',
            37: '-',
            CBJE: 0, // 差标金额
            40: '-',
          });
          let jys = getDays(getDataSource()[k].arrivalDate, getDataSource()[k].departureDate);
          let priceAvg =
            getDataSource()[k].status === 'CANCEL' &&
            getDataSource()[k].orderDetailDto.settlementPrice == 0
              ? (
                  -getDataSource()[k].orderDetailDto.totalPay /
                  getDays(getDataSource()[k].arrivalDate, getDataSource()[k].departureDate)
                ).toFixed(2)
              : (
                  getDataSource()[k].orderDetailDto.totalPay /
                  getDays(getDataSource()[k].arrivalDate, getDataSource()[k].departureDate)
                ).toFixed(2);

          // getDataSource()[k].orderDetailDto.totalPay / jys;
          data.value.id = k + 1;
          data.value.CCSQDH = getDataSource()[k].voucherId;
          data.value.DDH = getDataSource()[k].orderId;
          data.value.DDLX = '国内酒店';
          data.value.DDQK =
            getDataSource()[k].status === 'WAIT_CONFIRM'
              ? '等待确认'
              : getDataSource()[k].status === 'WAIT_PAY'
              ? '等待支付'
              : getDataSource()[k].status === 'CONFIRM'
              ? '已确认'
              : getDataSource()[k].status === 'NO_SHOW'
              ? '未入住'
              : getDataSource()[k].status === 'CHECK_OUT'
              ? '已离店'
              : getDataSource()[k].status === 'CANCEL'
              ? '已取消'
              : getDataSource()[k].status === 'CHECK_IN'
              ? '已入住'
              : '无效';
          data.value.GYSDH = getDataSource()[k].orderDetailDto.serverId;
          data.value.RZRBM =
            getDataSource()[k].examineApprove !== null
              ? getDataSource()[k].examineApprove.departmentId
              : '';
          data.value.SBYKK =
            getDataSource()[k].status === 'CANCEL' &&
            getDataSource()[k].orderDetailDto.settlementPrice == 0
              ? -getDataSource()[k].orderDetailDto.totalPay + getDataSource()[k].amount
              : getDataSource()[k].orderDetailDto.totalPay - getDataSource()[k].amount;

          // ? -getDataSource()[k].orderDetailDto.totalPay
          // : 100;
          data.value.TKJE =
            getDataSource()[k].status === 'CANCEL' &&
            getDataSource()[k].orderDetailDto.settlementPrice == 0
              ? getDataSource()[k].orderDetailDto.totalPay
              : '0';
          data.value.YDR = getDataSource()[k].orderDetailDto.names;
          data.value.YDRBM =
            getDataSource()[k].examineApprove !== null
              ? getDataSource()[k].examineApprove.departmentId
              : '-';
          data.value.YSZJ =
            getDataSource()[k].status === 'CANCEL' &&
            getDataSource()[k].orderDetailDto.settlementPrice == 0
              ? -getDataSource()[k].orderDetailDto.totalPay
              : getDataSource()[k].orderDetailDto.totalPay;
          data.value.ZDLX =
            getDataSource()[k].orderDetailDto.createBy === 'API接口' ? 'app' : '票务';
          data.value.ZJEBHFWD =
            getDataSource()[k].status === 'CANCEL' &&
            getDataSource()[k].orderDetailDto.settlementPrice == 0
              ? -getDataSource()[k].orderDetailDto.totalPay
              : getDataSource()[k].orderDetailDto.totalPay;
          data.value.accountNo = getDataSource()[k].accountNo;
          data.value.busiManage = getDataSource()[k].orderDetailDto.busiManage
            ? getDataSource()[k].orderDetailDto.busiManage
            : '-';
          data.value.certNo = getDataSource()[k].orderDetailDto.customers[0].certNo;
          data.value.channelName = getDataSource()[k].orderDetailDto.channelName;
          data.value.chucairenshu = '1';
          data.value.costDeptName = getDataSource()[k].orderDetailDto.customers[0].costDeptName
            ? getDataSource()[k].orderDetailDto.customers[0].costDeptName
            : '-';
          if (getDataSource()[k].status === 'CANCEL') {
            data.value.createTime = dayjs(getDataSource()[k].orderDetailDto.lastOptime).format(
              'YYYY-M-D HH:mm:ss',
            );
          } else {
            data.value.createTime = dayjs(getDataSource()[k].orderDetailDto.createTime).format(
              'YYYY-M-D HH:mm:ss',
            );
          }

          data.value.departureDate = getDataSource()[k].departureDate
            ? dayjs(getDataSource()[k].departureDate).format('YYYY-MM-DD')
            : '-';
          data.value.arrivalDate = getDataSource()[k].arrivalDate
            ? dayjs(getDataSource()[k].arrivalDate).format('YYYY-MM-DD')
            : '-';
          data.value.geoName = getDataSource()[k].orderDetailDto.geoName;
          data.value.hotelName = getDataSource()[k].orderDetailDto.hotelName;
          data.value.jianyeshu = jys.toString();
          data.value.mobile = getDataSource()[k].orderDetailDto.contact.mobile;
          data.value.money =
            getDataSource()[k].status === 'CANCEL' &&
            getDataSource()[k].orderDetailDto.settlementPrice == 0
              ? -getDataSource()[k].orderDetailDto.totalPay
              : getDataSource()[k].orderDetailDto.totalPay;
          data.value.names = getDataSource()[k].orderDetailDto.names;
          // data.value.orderId = getDataSource()[k].orderId;
          data.value.payType = getDataSource()[k].payType === 'SelfPay' ? '前台自付' : '月结';
          data.value.priceAvg = priceAvg.toString();

          data.value.roomCount = getDataSource()[k].orderDetailDto.roomCount;
          data.value.roomName = getDataSource()[k].orderDetailDto.roomName;
          data.value.status =
            getDataSource()[k].status === 'WAIT_CONFIRM'
              ? '等待确认'
              : getDataSource()[k].status === 'WAIT_PAY'
              ? '等待支付'
              : getDataSource()[k].status === 'CONFIRM'
              ? '已确认'
              : getDataSource()[k].status === 'NO_SHOW'
              ? '未入住'
              : getDataSource()[k].status === 'CHECK_OUT'
              ? '已离店'
              : getDataSource()[k].status === 'CANCEL'
              ? '已取消'
              : getDataSource()[k].status === 'CHECK_IN'
              ? '已入住'
              : '无效';
          data.value.travelType = '有审批单';

          data.value.amount = getDataSource()[k].amount ? getDataSource()[k].amount : 0;
          data.value.refundAmount =
            getDataSource()[k].status === 'CANCEL' &&
            getDataSource()[k].orderDetailDto.settlementPrice == 0
              ? getDataSource()[k].amount
              : '0';
          data.value.companyAmount =
            getDataSource()[k].status === 'CANCEL' &&
            getDataSource()[k].orderDetailDto.settlementPrice == 0
              ? -getDataSource()[k].companyAmount
              : getDataSource()[k].companyAmount;
          data.value.refundCompanyAmount =
            getDataSource()[k].status === 'CANCEL' &&
            getDataSource()[k].orderDetailDto.settlementPrice == 0
              ? getDataSource()[k].companyAmount
              : '0';
          data.value.serialNum = getDataSource()[k].serialNum ? getDataSource()[k].serialNum : '-';
          data.value.CBJE = getDataSource()[k].examineApprove.standardPrice;

          data.value.voucherId = getDataSource()[k].voucherId ? getDataSource()[k].voucherId : '-';
          data.value.recordId = getDataSource()[k].id;
          data.value.hotelLeave = getDataSource()[k].orderDetailDto.assist.hotelCategory
            ? getDataSource()[k].orderDetailDto.assist.hotelCategory
            : '-';
          data.value[22] = getDataSource()[k].amount === 0 ? '-' : getDataSource()[k].amount;
          orderArr.push(data.value);
        }

        jsonToSheetXlsx({
          data: orderArr,
          header,
          filename: '酒店订单(有审批单)' + dayjs(new Date()).format('YYYY-MM-DD') + '.xlsx',
          json2sheetOpts: {
            // 指定顺序
            header: [
              'id',
              'ZDLX',
              'createTime',
              'YDR',
              'CCSQDH',
              'DDH',
              'accountNo',
              'chucairenshu',
              'names',
              'mobile',
              'hotelName',
              'geoName',
              'arrivalDate',
              'departureDate',
              'roomCount',
              'jianyeshu',
              'hotelLeave',
              'roomName',
              'money',
              'priceAvg',
              'TKJE',
              '1',
              '2',
              '3',
              '4',
              '5',
              '6',
              '7',
              '8',
              'SBYKK',
              'YSZJ',
              'ZJEBHFWD',
              'DDQK',
              'channelName',
              '40',
              'payType',
              '9',
              'costDeptName',
              'voucherId',
              'busiManage',
              '10',
              'GYSDH',
              'status',
              '11',
              'DDLX',
              'YDRBM',
              'travelType',
              'amount',
              'refundAmount',
              'companyAmount',
              'refundCompanyAmount',
              'serialNum',
              '17',
              '18',
              '19',
              '20',
              '21',
              '22',
              '23',
              '24',
              '25',
              '26',
              '27',
              '28',
              '29',
              '30',
              '31',
              '32',
              '33',
              '34',
              '35',
              'RZRBM',
              '36',
              '37',
              'CBJE',
              'recordId',
              'certNo',
            ],
          },
        });
      }

      return {
        registerTable,
        customHeader,
      };
    },
  });
</script>
